﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_yprk : Form
    {
        public gl_yprk()
        {
            InitializeComponent();
        }

        public string rkfs;
        public static string rukudanhao;       //传值比 增加按钮 窗体
        string comStr;
        int queren_var = 0;               // 变量，控制“确认”按钮是否执行代码
        int delete_var = 0;               // 变量，控制“删除”按钮是否执行代码

        /// <summary>
        /// 快捷键，代码
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            if (keyData == (Keys.Alt | Keys.A))
            {
                button2_Click(null, null);
                return true;
            }

            //屏蔽 alt键，如果放在最前面，组合键（alt+其他键）将失效
            if ((keyData & Keys.Alt) == Keys.Alt)
            {
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }           

        #region 增加按钮
        private void button2_Click(object sender, EventArgs e)
        {
            gl_yprkd yprkd = new gl_yprkd();
            yprkd.Owner = this;
            rkfs = comboBox1.Text.ToString();
            yprkd.ShowDialog();
        }
        #endregion

        #region 刷新函数              ---显示左和右DatagridView
        private void xianshi_DatagridView()
        {
            //左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select t.rkdh,t.rkbz from YF_RK01 t where t.rkpb=0 and t.yfsb='" + mainform.yfsb + "' and " +
                "t.rkfs=(select t1.RKFS from YF_RKFS t1 where t1.yfsb='" + mainform.yfsb + "' and t1.fsmc='" + comboBox1.Text + "')";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            if (dt1.Rows.Count>0)
            {
                dataGridView1.Rows.Add(dt1.Rows.Count);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = dt1.Rows[i][1].ToString();
                }
            }

            //右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select t.rkdh,t.rkrq,t.rkbz from YF_RK01 t where t.rkpb=1 and t.yfsb='" + mainform.yfsb + "' and " +
                "t.rkfs=(select t1.RKFS from YF_RKFS t1 where t1.yfsb='" + mainform.yfsb + "' and t1.fsmc='" + comboBox1.Text + "')";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            if (dt2.Rows.Count>0)
            {
                dataGridView2.Rows.Add(dt2.Rows.Count);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dataGridView2.Rows[i].Cells[0].Value = dt2.Rows[i][0].ToString();
                    dataGridView2.Rows[i].Cells[1].Value = Convert.ToDateTime(dt2.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView2.Rows[i].Cells[2].Value = dt2.Rows[i][2].ToString();
                }
            }

            #region datagridView默认不选中单元格，使之成蓝色
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = false;
            }
            if (dataGridView2.Rows.Count > 0)
            {
                dataGridView2.Rows[0].Selected = false;
            }
            #endregion
        }
        #endregion

        #region 打开入库处理load
        private void gl_rkcl_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView2.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView2.AllowUserToResizeColumns = false;
            foreach (DataGridViewColumn c in dataGridView1.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            foreach (DataGridViewColumn c in dataGridView2.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            //入库方式
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = new DataTable();
            comStr = "select fsmc from YF_RKFS where yfsb=" + mainform.yfsb + "";
            dt.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBox1.Items.Add(dt.Rows[i][0]);
            }
            comboBox1.Text = dt.Rows[1][0].ToString();

            xianshi_DatagridView();
        }
        #endregion

        #region 删除按钮
        private void button3_Click(object sender, EventArgs e)
        {
            if (delete_var == 1)
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    if (MessageBox.Show("是否删除入库单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？",
                        "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        string Delete_rkdh = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                        try
                        {
                            //删除两张表 ---入库表和入库明细表
                            string comStr = "delete from YF_RK01 where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "';" +
                                "delete from YF_RK02 where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            xianshi_DatagridView();
                            MessageBox.Show("删除入库单" + "" + Delete_rkdh + "" + "成功！");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                    else
                    {
                        delete_var = 0;
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {

                            dataGridView1.Rows[i].Selected = false;
                        }
                    }
                }
            }
        }
        #endregion  

        #region 刷新按钮
        private void button1_Click(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }        
        #endregion

        #region 确认按钮
        private void button4_Click(object sender, EventArgs e)
        {
            if (queren_var == 1)
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    if (MessageBox.Show("是否确认入库单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", 
                        "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            comStr = "update YF_RK01 set rKPB='1',fhy='" + Program.ygbh + "' where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);

                            try
                            {                                 
                                comStr = "select rksl,yfsb,ypxh,ypcd,ypph,ypxq,lsjg,lsje from YF_RK02 "+
                                    "where rkdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                                DataTable dt2=new DataTable ();
                                dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                                for (int i = 0; i < dt2.Rows.Count; i++)
                                {                               
                                    decimal rksl = Convert.ToDecimal(dt2.Rows[i][0]);
                                    string yfsb = dt2.Rows[i][1].ToString();
                                    string ypxh = dt2.Rows[i][2].ToString();
                                    string ypcd = dt2.Rows[i][3].ToString();
                                    string ypph = dt2.Rows[i][4].ToString();
                                    string ypxq = dt2.Rows[i][5].ToString();
                                    string lsjg = dt2.Rows[i][6].ToString();
                                    string lsje = dt2.Rows[i][7].ToString();
                                    //增加yf_kcmx的库存ypsl
                                    comStr = "update t1 set t1.ypsl=t1.ypsl+'" + rksl + "' from yf_kcmx t1 where " +
                                    "t1.ypxh='" + ypxh + "' and t1.yfsb='" + yfsb + "' and t1.ypph='" + ypph + "'";
                                    int res = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                                    //如果update不到，yf_kcmx插入记录
                                    if (res<1)
                                    {
                                        //comStr = "select max(ypph)+1 from yf_kcmx";
                                        //string ypph2 = (SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null)).ToString();

                                        comStr = "insert into yf_kcmx(ypph,ypsl,yfsb,ypxh,ypcd,ypph,ypxq,lsjg,lsje) select max(ypph)+1,'" + rksl + "'," +
                                            "'" + yfsb + "','" + ypxh + "','"+ypcd+"','"+ypph+"','"+ypxq+"','"+lsjg+"','"+lsje+"' from yf_kcmx";
                                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message.ToString());                                
                            }
                            xianshi_DatagridView();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                    else
                    {
                        queren_var = 0;
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {

                            dataGridView1.Rows[i].Selected = false;
                        }
                    }
                }
            }
        }
        #endregion   

        #region 显示入库单，查看入库单信息
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            rukudanhao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            gl_yprkd_zuo yprkd_zuo = new gl_yprkd_zuo();
            yprkd_zuo.Owner = this;
            yprkd_zuo.ShowDialog();
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            rukudanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_yprkd_you yprkd_you = new gl_yprkd_you();
            yprkd_you.Owner = this;
            yprkd_you.ShowDialog();
        }
        #endregion

        #region datagridView失去焦点，单元格不显示蓝色
        private void dataGridView1_Leave(object sender, EventArgs e)
        {            
            if (!button4.Focused)
            {
                queren_var = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    dataGridView1.Rows[i].Selected = false;
                }
            }

            if (!button3.Focused)
            {
                delete_var = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    dataGridView1.Rows[i].Selected = false;
                }
            }

            //方法2：
            //this.dataGridView1.ClearSelection();
            //this.dataGridView1.TabStop = false;

            //方法3：this.dataGridView1.CurrentCell = null;
        }

        private void dataGridView2_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                dataGridView2.Rows[i].Selected = false;
            }
        }
        #endregion   
     
        #region 确认按钮
        private void button5_Click(object sender, EventArgs e)
        {
            //rukudanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            //gl_yprkd_you yprkd_you = new gl_yprkd_you();
            //yprkd_you.Owner = this;
            //yprkd_you.ShowDialog();
        }
        #endregion

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //变量，控制“确认”按钮、“删除”按钮是否执行代码
        private void dataGridView1_Enter(object sender, EventArgs e)
        {
            queren_var = 1;
            delete_var = 1;
        }

        //按入库方式，显示左、右datagridView
        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }

        //窗体重新获取焦点，发生的刷新事件
        private void gl_yprk_Paint(object sender, PaintEventArgs e)
        {
            xianshi_DatagridView();
        }
    }
}
